package com.jun.config.shiro;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;


/**
 * @author zjp
 */
@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);


        /**
         * anon:无需认证
         * authc: 必须认证
         * user: 记住我才能使用
         * perms: 拥有权对某个资源的权限
         * role 角色权限
         */
        System.out.println("授权");
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/html/reset.html","anon");
        filterMap.put("/html/**","authc");
        bean.setFilterChainDefinitionMap(filterMap);
        bean.setLoginUrl("/html/login.html");
        return bean;

    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager =new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }


    @Bean(name = "userRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }


}
